//
//  SEGIntegralManager.m
//  ExcellentHome
//
//  Created by 刘山 on 2017/2/10.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGIntegralManager.h"

#if __has_include(<Commom/SEGToolsHeader.h>)
#import <Commom/SEGToolsHeader.h>
#else
#import "SEGToolsHeader.h"
#endif

@implementation SEGIntegralManager


/// 积分统计（昨日，今日）
-(void)requestIntegralStatisticsUserId:(NSString*)userId //用户ID
                              Finished:(SEGNetworkReponse)finished{

    NSDictionary *params = @{
                             @"userId": ValueToString(userId)
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/integral-api/integral/ordinaryCoin/statistics.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        NSDictionary* statistics = [result valueForKey:@"data"];
        
        self.seg_segstatisticsmodel = [SEGStatisticsModel initWithDictionary:statistics];
        
        finished(YES,result, codeType, errorMessage);
    }];
}

/// 加载兑换成的公告信息
-(void)requestIntegralNoticeWithPageSize:(NSString *)pageSize
                                  pageNo:(NSString *)pageNo
                                Finished:(SEGNetworkReponse)finished
{
    NSDictionary *params = @{@"pageSize":pageSize,@"pageNo":pageNo};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/integral-api/integral/ordinaryCoin/notice.json",nil];
    
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        NSDictionary* data = [result valueForKey:@"data"];
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSArray* notices = [data valueForKey:@"notices"];
            
            NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:notices.count];
            
            for (NSDictionary* dic in notices) {
                
                SEGNoticeModel* _model = [SEGNoticeModel initWithDictionary:dic];
                
                [arrM addObject:_model];
            }
            
            self.seg_segnoticemodel = arrM;
            self.noticePageNo = [data objectForKey:@"pageNo"];
            self.noticeTotalPage = [data objectForKey:@"totalPage"];
            
            finished(YES,result, codeType, errorMessage);
        }else {
            finished(NO,result, [result valueForKey:@"code"], errorMessage);
        }
    }];
}

///  6.获取个人配送地址
- (void)requestMyAddressFinished:(SEGNetworkReponse)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/getMyAddress",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        NSArray* arr = [result valueForKey:@"data"];
        
        if (arr && arr.count <= 0) {
            self.seg_segaddressmodel = [NSArray array];
            finished(NO,result, codeType, @"无配送地址"); //数据返回正常，但是数据为空
            return;
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:arr.count];
        
        for (NSDictionary* dic in arr) {
            
            SEGAddressModel* _model = [SEGAddressModel initWithDictionary:dic];
            
            [arrM addObject:_model];
        }
        
        self.seg_segaddressmodel = arrM;
        
        finished(YES,result, codeType, errorMessage);
    }];
}

/**
 * 加载确认兑换页信息
 * @author Wendy
 * @param pid  商品ID
 */
-(void)requestIntegralExchangeConfirmInfoWithId:(NSString *)pid finished:(SEGNetworkReponse)finished
{
    NSDictionary *params = @{@"id":pid};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/integral-api/v2/app/product/confirm",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            NSDictionary* data = [result valueForKey:@"data"];
            self.exchangeConfirmModel = [SEGIntegralExchangeConfirmModel initWithDictionary:data];
        }
        
        finished(isSuccessed,result, codeType, errorMessage);
    }];
}

/**
 * 加减数量判断是否足够
 * @author Wendy
 * @param pid  商品ID
 */
-(void)requestIntegralExchangeJudgeQuantityWithId:(NSString *)pid
                                         quantity:(NSUInteger)quantity
                                         finished:(SEGNetworkReponse)finished
{
    NSDictionary *params = @{@"id":pid,@"quantity":[NSString stringWithFormat:@"%ld",(unsigned long)quantity]};
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/integral-api/v2/app/product/judgeQuantity",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        finished(isSuccessed,result, codeType, errorMessage);
    }];
}

/**
 * 积分商城商品抢兑
 * @author Wendy
 * @param params 信息字典
 * @param finished 回调结果
 */
- (void)requestExchangeConfirmWithParams:(NSDictionary *)params finished:(SEGNetworkReponse)finished
{
    //kExchange
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/integral-api/v2/app/product/exchange",nil];
    [SEGHTTPBusinessManager seg_requestJSONWithMethod:POST
                                            URLString:urlString
                                           parameters:params
                                             finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                                                 
                                                 finished(isSuccessed, result,codeType, errorMessage);
                                                 
                                             }];
}

/**
 *  请求钱包-新的添加模型的请求
 */
- (void)requestMyCardV2WithDict:(NSDictionary*)params Finished:(SEGNetworkReponse)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/balalce/getBanalces",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        NSArray* data = [result valueForKey:@"data"];
        if (![data isKindOfClass:[NSArray class]]) {
            finished(NO,result, @"-1", @"暂时没有数据");
            return;
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:data.count];
        
        for (NSDictionary* dic in data) {
            
            SEGBanalcesModel* _model = [SEGBanalcesModel yy_modelWithDictionary:dic];
            
            [arrM addObject:_model];
        }
        
        self.seg_segbanalcesmodel = arrM;
        
        finished(YES,result, codeType, errorMessage);
    }];
}

- (void)requestMyBalanceForHbDict:(NSDictionary*)params Finished:(SEGNetworkReponse)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/balalce/getBalanceForHb",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        NSArray* data = [result valueForKey:@"data"];
        
        if (![data isKindOfClass:[NSArray class]]) {
            finished(NO,result, @"-1", @"暂时没有数据");
            return;
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:data.count];
        
        for (NSDictionary* dic in data) {
            
            SEGBalanceForHbModel* _model = [SEGBalanceForHbModel initWithDictionary:dic];
            
            [arrM addObject:_model];
        }
        
        self.seg_segbalanceforhbmodel = arrM;
        
        finished(YES,result, codeType, errorMessage);
        
    }];
}

- (SEGStatisticsModel*)seg_segstatisticsmodel
{
    if (!_seg_segstatisticsmodel) {
        _seg_segstatisticsmodel = [[SEGStatisticsModel alloc] init];
    }
    return _seg_segstatisticsmodel;
}

- (NSArray<SEGNoticeModel*>*)seg_segnoticemodel
{
    if (!_seg_segnoticemodel)
    {
        _seg_segnoticemodel = [NSArray array];
    }
    return _seg_segnoticemodel;
}

- (SEGIntegralBehaviorModel*)seg_segintegralbehaviormodel
{
    if (!_seg_segintegralbehaviormodel) {
        _seg_segintegralbehaviormodel = [[SEGIntegralBehaviorModel alloc] init];
    }
    return _seg_segintegralbehaviormodel;
}

- (SEGIntegralExchangeConfirmModel*)exchangeConfirmModel
{
    if (!_exchangeConfirmModel) {
        _exchangeConfirmModel = [[SEGIntegralExchangeConfirmModel alloc] init];
    }
    return _exchangeConfirmModel;
}

- (NSArray<SEGBanalcesModel*>*)seg_segbanalcesmodel
{
    if (!_seg_segbanalcesmodel)
    {
        _seg_segbanalcesmodel = [NSArray array];
    }
    return _seg_segbanalcesmodel;
}

- (NSArray<SEGBalanceForHbModel*>*)seg_segbalanceforhbmodel
{
    if (!_seg_segbalanceforhbmodel)
    {
        _seg_segbalanceforhbmodel = [NSArray array];
    }
    return _seg_segbalanceforhbmodel;
}

- (NSArray<SEGAddressModel*>*)seg_segaddressmodel
{
    if (!_seg_segaddressmodel) {
        _seg_segaddressmodel = [NSArray array];
    }
    return _seg_segaddressmodel;
}

@end

